package com.hrb.monkey.frament;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.github.chrisbanes.photoview.PhotoView;
import com.hrb.monkey.R;
import com.hrb.monkey.utils.ImageLoaderManager;
import com.hrb.monkey.utils.LineChartView;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class SecondFragment extends Fragment {


    @BindView(R.id.second_fragment_imageView)
    ImageView secondFragmentImageView;
    @BindView(R.id.second_fragment_phoView)
    PhotoView secondFragmentPhoView;
    @BindView(R.id.line_chart)
    LineChartView lineChart;
    private Unbinder unbinder;

    private ImageLoaderManager mImagerLoader;
    private List<String> xValues;
    private List<Integer> yValues;

    public SecondFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mImagerLoader = ImageLoaderManager.getInstance(getContext());
        View view = inflater.inflate(R.layout.fragment_second, container, false);
        unbinder = ButterKnife.bind(this, view);
        secondFragmentPhoView.setImageResource(R.drawable.xiaohuangren);
        mImagerLoader.displayImage(secondFragmentImageView, "http://www.yuhuagu.com/uploads/allimg/121218/43_121218074108_1.jpg");
        initLineChartValue();
        //new DownloadImageTask(secondFragmentImageView).execute("http://www.yuhuagu.com/uploads/allimg/121218/43_121218074108_1.jpg");
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initLineChartValue(){
        xValues=new ArrayList<String>(){{add("壹");add("贰");add("叁");add("肆");}};
        yValues=new ArrayList<Integer>(){{add(1);add(30);add(60);add(100);}};
        for(int i=0;i<100;i++){
            int v=i;
            xValues.add(i+"");
            yValues.add(v);
        }
        lineChart.setXValues(xValues);
        lineChart.setYValues(yValues);
    }

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }

}
